#pragma once
#include <fstream>
#include "Common.hpp"
#include "Log.hpp"

using namespace myLog;

//用来实现一些常用的方法 -> 全是静态成员函数
class Util{
public:
//从字符串str中，读取出sep前面的串，带出去给outbuffer
    static bool ReadOneLine(std::string& str, std::string* outbuffer, const std::string sep){
        size_t pos = str.find(sep);
        if(pos == std::string::npos) return false;
        else{
            *outbuffer = str.substr(0, pos);
            str.erase(0, pos + sep.size());
            return true;
        }
    }

    static int GetFileSize(const std::string& file_path){
        std::ifstream in(file_path, std::ios::binary);
        if(!in.is_open()) return -1;
        
        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);
        in.close();
        return filesize;
    }

    static bool ReadFileContent(const std::string& file_path, std::string* out){
        //这里不能单纯的使用文本读取！应当使用二进制读取 -> 因为可能有图片，有视频...这些都是二进制的
        std::ifstream in(file_path);
        if(!in.is_open()) return false;
        //这个时候就知道，大致有多少二进制数据要被读取出来了 -> 可以获取文件的字节数
        int readsize = GetFileSize(file_path);
        if(readsize <= 0) return false; 

        out->resize(readsize);

        //使用二进制读取
        in.read((char*)out->c_str(), readsize);

        in.close();
        return true;
    }

private:
};


//这里搞多一个类，以便于Content-Type字段使用
class MimeTypes {
private:
    std::unordered_map<std::string, std::string> mimeTypes;

public:
    MimeTypes() {
        // 初始化常用 MIME 类型
        // 文本类型
        mimeTypes[".txt"] = "text/plain";
        mimeTypes[".html"] = "text/html";
        mimeTypes[".htm"] = "text/html";
        mimeTypes[".css"] = "text/css";
        mimeTypes[".js"] = "text/javascript";
        mimeTypes[".csv"] = "text/csv";
        mimeTypes[".xml"] = "text/xml";
        
        // 图像类型
        mimeTypes[".jpg"] = "image/jpeg";
        mimeTypes[".jpeg"] = "image/jpeg";
        mimeTypes[".png"] = "image/png";
        mimeTypes[".gif"] = "image/gif";
        mimeTypes[".svg"] = "image/svg+xml";
        mimeTypes[".webp"] = "image/webp";
        mimeTypes[".ico"] = "image/x-icon";
        
        // 应用程序类型
        mimeTypes[".json"] = "application/json";
        mimeTypes[".pdf"] = "application/pdf";
        mimeTypes[".zip"] = "application/zip";
        mimeTypes[".doc"] = "application/msword";
        mimeTypes[".docx"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        
        // 多媒体类型
        mimeTypes[".mp3"] = "audio/mpeg";
        mimeTypes[".ogg"] = "audio/ogg";
        mimeTypes[".mp4"] = "video/mp4";
        mimeTypes[".mov"] = "video/quicktime";
        mimeTypes[".avi"] = "video/x-msvideo";
        
        // 字体类型
        mimeTypes[".woff"] = "font/woff";
        mimeTypes[".woff2"] = "font/woff2";
        mimeTypes[".ttf"] = "font/ttf";
        mimeTypes[".otf"] = "font/otf";
    }

    // 根据文件扩展名获取 MIME 类型
    std::string getMimeType(const std::string& fileExtension) const {
        auto it = mimeTypes.find(fileExtension);
        if (it != mimeTypes.end()) {
            return it->second;
        }
        // 默认返回二进制流类型
        return "application/octet-stream";
    }

    // 添加自定义 MIME 类型
    void addMimeType(const std::string& fileExtension, const std::string& mimeType) {
        mimeTypes[fileExtension] = mimeType;
    }
};
